package com.lenovo.agricultureclient.request;

import org.json.JSONException;
import org.json.JSONObject;
import org.w3c.dom.Element;

import com.lenovo.agricultureclient.bean.ControllerStatus;
import com.lenovo.agricultureclient.util.Util;
/**
 * 获取控制器状态的请求类
 * @author zongbingwu
 *
 */
public class GetControllerStatusRequest extends AgricultureRequest 
{
	//action name
	private static final String ACTION = "getContorllerStatus";
	//存放控制器状态
	private ControllerStatus controllerStatus;

	public ControllerStatus getControllerStatus() {
		return controllerStatus;
	}
	public void setControllerStatus(ControllerStatus controllerStatus) {
		this.controllerStatus = controllerStatus;
	}

	@Override
	protected String getActionName() {
		return ACTION;
	}
	
	//jason协议时的获取body函数，不必携带额外参数
	@Override
	protected String onGetJasonBody() 
	{
		return "{}"; //不能为空字符串
	}
	
	//soap协议时的获取body函数，不必携带额外参数
	@Override
	protected String onGetSoapBody() 
	{
		return Util.getSoapXml("");
	}
	
	//jason协议时解析回应函数
	@Override
	protected void onJasonParese(String responseStr) 
	{
		try 
		{
			JSONObject jsonRespObj = new JSONObject(responseStr);
			if(jsonRespObj!=null && controllerStatus!=null)
			{
				if(jsonRespObj.has("result"))
				{
					String result = jsonRespObj.getString("result");
					result = result.toLowerCase();
					//判断请求是否成功
					if(result.equals("ok")){
						isSuccess = true;
					} else {
						isSuccess = false;
					}
				}
				//蜂鸣器控制器
				if(jsonRespObj.has("Buzzer")){
					controllerStatus.setBuzzer(jsonRespObj.getInt("Buzzer"));
				}
				//水泵控制器
				if(jsonRespObj.has("WaterPump")){
					controllerStatus.setWaterPump(jsonRespObj.getInt("WaterPump"));
				}
				//路灯控制器
				if(jsonRespObj.has("Roadlamp")){
					controllerStatus.setRoadlamp(jsonRespObj.getInt("Roadlamp"));
				}
				//风扇控制器
				if(jsonRespObj.has("Blower")){
					controllerStatus.setBlower(jsonRespObj.getInt("Blower"));
				}
			}
			else {
				isSuccess = false;
			}
		} 
		catch (JSONException e) 
		{
			isSuccess = false;
			e.printStackTrace();
		}
	}
	
	@Override
	protected void onSoapParese(String responseStr) 
	{
		//从完整的soap协议内容中解析出soap body内容
		Element soapBodyEle = Util.getSoapBodyElement(responseStr);
		if(soapBodyEle!=null && controllerStatus!=null)
		{
			try 
			{
				String result = Util.getChildElementValueStr(soapBodyEle, "result");
				result = result.toLowerCase();
				//判断请求是否成功
				if(result.equals("ok")){
					isSuccess = true;
				} else {
					isSuccess = false;
				}
				//蜂鸣器控制器
				Element buzzer = (Element)soapBodyEle.getElementsByTagName("Buzzer").item(0);
				if(buzzer != null){
					controllerStatus.setBuzzer(Integer.parseInt(buzzer.getFirstChild().getNodeValue()));
				}
				//水泵控制器
				Element waterPump = (Element)soapBodyEle.getElementsByTagName("WaterPump").item(0);
				if(waterPump!=null){
					controllerStatus.setWaterPump(Integer.parseInt(waterPump.getFirstChild().getNodeValue()));
				}
				//路灯控制器
				Element roadlamp = (Element)soapBodyEle.getElementsByTagName("Roadlamp").item(0);
				if(roadlamp!=null){
					controllerStatus.setRoadlamp(Integer.parseInt(roadlamp.getFirstChild().getNodeValue()));
				}
				//风扇控制器
				Element blower = (Element)soapBodyEle.getElementsByTagName("Blower").item(0);
				if(blower!=null){
					controllerStatus.setBlower(Integer.parseInt(blower.getFirstChild().getNodeValue()));
				}
				
			} catch (Exception e) {
				e.printStackTrace();
				isSuccess = false;
			}
		} else {
			isSuccess = false;
		}
	}

}
